package com.lagou.controller;

import com.lagou.db.Product;
import com.lagou.es.EsService;
import com.lagou.mapper.ProductMapper;
import com.lagou.pojo.ProductQuery;
import com.lagou.pojo.ProductVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {


    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private EsService service;

    @GetMapping("/test")
    public Product test(){
        return productMapper.selectById(10);
    }





    @PostMapping("/query")
    public List<ProductVo> query(@RequestBody ProductQuery productQuery){
        List<com.lagou.pojo.Product> products = service.query(productQuery);

        List<ProductVo> productList = new ArrayList<>();
        for(com.lagou.pojo.Product p : products){
            System.out.println("转换之前："+p);
            System.out.println("转换之后: "+ com.lagou.pojo.Product.converter(p));
            ProductVo productVo = new ProductVo();

            BeanUtils.copyProperties(p, productVo);
            productList.add(productVo);
        }

        System.out.println(productList);
        return productList;
    }


}
